
import com.github.kwhat.jnativehook.GlobalScreen;
import com.github.kwhat.jnativehook.NativeHookException;
import com.github.kwhat.jnativehook.keyboard.NativeKeyEvent;
import com.github.kwhat.jnativehook.keyboard.NativeKeyListener;


public class GlobalKeyListener implements NativeKeyListener {

    // 键盘按下事件
    public void nativeKeyPressed(NativeKeyEvent e) {
        System.out.println("Key Pressed(按下): " + NativeKeyEvent.getKeyText(e.getKeyCode()));
    }

    // 键盘释放事件
    public void nativeKeyReleased(NativeKeyEvent e) {
        System.out.println("Key Released(释放): " + NativeKeyEvent.getKeyText(e.getKeyCode()));
    }

    // 键盘输入字符事件
    public void nativeKeyTyped(NativeKeyEvent e) {
        // 通常不需要在此处处理，因为已经通过 `nativeKeyPressed` 和 `nativeKeyReleased` 处理了
    }

    public static void main(String[] args) {
        try {
            // 注册全局钩子
            GlobalScreen.registerNativeHook();
        } catch (NativeHookException ex) {
            System.err.println("Failed to register the native hook!");
            ex.printStackTrace();
        }

        // 添加键盘监听器
        GlobalScreen.addNativeKeyListener(new GlobalKeyListener());
    }
}
